package com.xinqi.modules.user.users.third.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.core.enums.UeUserThirdBindTypeEnum;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.modules.user.users.third.convert.UserThirdBindConvert;
import com.xinqi.modules.user.users.third.domain.UserThirdBindEntity;
import com.xinqi.modules.user.users.third.manager.UserThirdBindManager;
import com.xinqi.modules.user.users.third.service.UserThirdBindService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * 第三方用户绑定表服务层实现
 *
 * @author: yyzhen
 * @date: 2023/07/17
 */
@Validated
@Service("userThirdBindService")
@RequiredArgsConstructor
public class UserThirdBindServiceImpl implements UserThirdBindService {
    private final UserThirdBindConvert userThirdBindConvert;
    private final UserThirdBindManager userThirdBindManager;

    /**
     * 根据类型和值查询第三方绑定是否存在
     *
     * @param type 类型
     * @param code 值
     * @return Boolean
     */
    @Override
    public Boolean isExistsByTypeAndCode(String type, String code, Long id) {
        LambdaQueryWrapper<UserThirdBindEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserThirdBindEntity::getType, type).eq(UserThirdBindEntity::getCode, code);
        if (id != null) {
            wrapper.ne(UserThirdBindEntity::getId, id);
        }
        return userThirdBindManager.count(wrapper) > 0;
    }


    /**
     * 解绑QQ
     *
     * @Title:
     * @MethodName: unbindQQ
     * [userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/11 18:47
     */
    @Override
    public Boolean unbindQQ(Long userId) {
        LambdaQueryWrapper<UserThirdBindEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserThirdBindEntity::getType, UeUserThirdBindTypeEnum.QQ.getType()).eq(UserThirdBindEntity::getUserId, userId);
        return userThirdBindManager.remove(wrapper);
    }

    /**
     * 绑定QQ
     *
     * @Title:
     * @MethodName: bindQQ
     * [userId, code]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/11 18:51
     */
    @Override
    public Boolean bindQQ(Long userId, String code) {
        //查找是否绑定QQ
        CommonErrors.BAD_REQUEST.check(ObjectUtils.isEmpty(userThirdBindManager.findByTypeAndUserId(UeUserThirdBindTypeEnum.QQ.getType(), userId)), MessageUtils.message("sys.third.user.QQ.message"));
        //查找该QQ是否被绑定
        CommonErrors.BAD_REQUEST.check(!isExistsByTypeAndCode(UeUserThirdBindTypeEnum.QQ.getType(), code, userId), MessageUtils.message("sys.third.QQ.message"));

        UserThirdBindEntity entity = new UserThirdBindEntity();
        entity.setUserId(userId);
        entity.setType(UeUserThirdBindTypeEnum.QQ.getType());
        entity.setCode(code);
        return userThirdBindManager.save(entity);
    }

    /**
     * 解绑微信
     *
     * @Title:
     * @MethodName: unbindWeChat
     * [userId]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/11 18:47
     */
    @Override
    public Boolean unbindWeChat(Long userId) {
        LambdaQueryWrapper<UserThirdBindEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserThirdBindEntity::getType, UeUserThirdBindTypeEnum.WECHAT.getType()).eq(UserThirdBindEntity::getUserId, userId);
        return userThirdBindManager.remove(wrapper);
    }

    /**
     * 绑定微信
     *
     * @Title:
     * @MethodName: bindWeChat
     * [userId, code]
     * @Return java.lang.Boolean
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/11 18:52
     */
    @Override
    public Boolean bindWeChat(Long userId, String code) {
        //查找是否绑定微信
        CommonErrors.BAD_REQUEST.check(ObjectUtils.isEmpty(userThirdBindManager.findByTypeAndUserId(UeUserThirdBindTypeEnum.WECHAT.getType(), userId)), MessageUtils.message("sys.third.user.WeChat.message"));
        //查找该微信是否被绑定
        CommonErrors.BAD_REQUEST.check(!isExistsByTypeAndCode(UeUserThirdBindTypeEnum.WECHAT.getType(), code, userId), MessageUtils.message("sys.third.WeChat.message"));

        UserThirdBindEntity entity = new UserThirdBindEntity();
        entity.setUserId(userId);
        entity.setType(UeUserThirdBindTypeEnum.WECHAT.getType());
        entity.setCode(code);
        return userThirdBindManager.save(entity);
    }

}
